package com.pand.controller;

import com.pand.constant.SystemConstants;
import com.pand.controller.common.BaseController;
import com.pand.mysql.service.OrderService;
import com.pand.mysql.service.TrackService;
import com.pand.service.VehicleService;
import com.pand.util.Response;
import com.pand.vo.VehicleLocationVo;
import com.pand.vo.VehicleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by lh on 2017/11/3.
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController extends BaseController {
    @Autowired
    VehicleService vehicleService;

    @Autowired
    @Qualifier(value = "orderService")
    OrderService orderService;

    @Autowired
    TrackService trackService;

    @ApiOperation(value = "车辆信息")
    @GetMapping("/info")
    public Response info() {
        long totalAmount = vehicleService.getVehicleTotalAmount();
        long idleAmount = vehicleService.getVehicleAmount(SystemConstants.VEHICLE_OPERATE_STATUS_IDLE);
        long rentingAmount = vehicleService.getVehicleAmount(SystemConstants.VEHICLE_OPERATE_STATUS_USED);
        long pauseAmount = vehicleService.getOtherVehicleAmount();
        VehicleVo vo = new VehicleVo();
        vo.setIdleAmount(idleAmount);
        vo.setRentingAmount(rentingAmount);
        vo.setTotalAmount(totalAmount);
        vo.setPauseAmount(pauseAmount);
        return Response.ok().data(vo);
    }

//    @ApiOperation(value = "车辆实时位置信息")
//    @GetMapping("/location")
//    public Response location() {
//        Date now = new Date();
//        List<Track> tracks = orderService.findOrderByTime(now).stream().map(order -> {
//            Track track = trackService.findByVin(order.getVin()).stream().max(Comparator.comparing(t -> t.getGpsTime())).get();
//            return track;
//        }).collect(Collectors.toList());
//        return Response.ok().data(tracks);
//    }

    @ApiOperation(value = "车辆实时位置信息")
    @GetMapping("/location/{page}/{size}")
    public Response location(@PathVariable int page, @PathVariable int size) {
        if (page < 1) {
            return Response.fail().data("page必须是大于0的自然数");
        }
        List<VehicleLocationVo> locationVos = vehicleService.findRealLocation(page - 1, size).parallelStream().map(object -> {
            VehicleLocationVo vo = new VehicleLocationVo();
            Object[] objects = (Object[]) object;
            if (objects[0] != null) {
                vo.setVehicleId(Long.valueOf(objects[0].toString()));
            }
            if (objects[1] != null) {
                vo.setLicense(objects[1].toString());
            }
            if (objects[2] != null) {
                vo.setVinCode(objects[2].toString());
            }
            if (objects[3] != null) {
                vo.setLongtitude(Double.valueOf(objects[3].toString()));
            }
            if (objects[4] != null) {
                vo.setLatitude(Double.valueOf(objects[4].toString()));
            }
            if (objects[5] != null) {
                vo.setMileage(Double.valueOf(objects[5].toString()));
            }
            return vo;
        }).collect(Collectors.toList());
        return Response.ok().data(locationVos);
    }

}
